package team.first.movieapiv2pro.dao.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import dev.morphia.Datastore;
import org.springframework.stereotype.Repository;
import team.first.movieapiv2pro.dao.AuthorizationDAO;
import team.first.movieapiv2pro.doc.UserDoc;
import team.first.movieapiv2pro.domain.User;
import team.first.movieapiv2pro.util.MongoDBUtil;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapiv2pro.dao.impl
 * @Date: 2021/12/26 9:09
 * @Description:
 */
@Repository
public class AuthorizationDAOImpl implements AuthorizationDAO {
    @Override
    public boolean register(User user) {
        Datastore datastore = MongoDBUtil.getDataStore();
        datastore.getMapper().mapPackage("team.first.movieapiv2pro.doc");

        UserDoc userDoc = new UserDoc();
        BeanUtil.copyProperties(user, userDoc);
        try {
            datastore.save(userDoc);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public User selectUser(User user) {
        DBCollection collection = MongoDBUtil.getCollection("user");
        // 查询用户信息
        // 建立查询条件
        BasicDBObject filter_dbobject = new BasicDBObject();
        filter_dbobject.put("email", user.getEmail());
        filter_dbobject.put("password", user.getPassword());

        // 条件查询
        DBCursor cursor = collection.find(filter_dbobject).sort(new BasicDBObject("id", 1));

        // 将结果转为集合
        List<DBObject> dbObjectList = cursor.toArray();
        // 属性拷贝
        List<User> userList = BeanUtil.copyToList(dbObjectList, User.class);

        // 登录成功
        if (userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }

    @Override
    public boolean isExist(String email) {
        DBCollection collection = MongoDBUtil.getCollection("user");
        // 查询用户信息
        // 建立查询条件
        BasicDBObject filter_dbobject = new BasicDBObject();
        filter_dbobject.put("email", email);

        // 条件查询
        DBCursor cursor = collection.find(filter_dbobject);

        // 将结果转为集合
        List<DBObject> dbObjectList = cursor.toArray();

        if (dbObjectList == null) {
            return false;
        }

        return dbObjectList.size() > 0;
    }

    @Override
    public User selectById(Integer id) {
        DBCollection collection = MongoDBUtil.getCollection("user");
        // 查询用户信息
        // 建立查询条件
        BasicDBObject filter_dbobject = new BasicDBObject();
        filter_dbobject.put("id", id);

        // 条件查询
        DBCursor cursor = collection.find(filter_dbobject);

        // 将结果转为集合
        List<DBObject> dbObjectList = cursor.toArray();

        if (dbObjectList == null || dbObjectList.size() < 1) {
            return null;
        }

        return BeanUtil.copyProperties(dbObjectList.get(0), User.class);
    }
}
